<template>
  <div>
    <van-form @submit="UpdatePwd">
      <van-row>
      <van-col span="8"></van-col>
      <van-col span="8">
        <van-icon name="http://rul4y8e5f.hn-bkt.clouddn.com/%E4%BA%91%E5%AF%86%E7%A0%81.png" size="5rem"></van-icon>
      </van-col>
      <van-col span="8"></van-col>
    </van-row>
      <van-field
        v-model="PasswordDTO.oldPassword"
        type="password"
        name="当前密码"
        label="当前密码"
        placeholder="当前密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="PasswordDTO.newPassword"
        type="password"
        name="新密码"
        label="新密码"
        placeholder="新密码"
        :rules="[{ required: true, message: '请填写新密码' }]"
      />
      <van-field
        v-model="Password2"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        @blur="pwd2pwd()"
        :rules="[{ required: true, message: '请填写确认密码' }]"
      />
      
      <div style="margin: 16px;">
    <van-button round block type="info" icon="http://rul4y8e5f.hn-bkt.clouddn.com/%E4%BF%9D%E5%AD%98.png" 
    native-type="submit" >保存</van-button>
  </div>
    </van-form>
  </div>
</template>

<script>
import UserAPI from '@/api/UserAPI'
export default {
  name: 'UpdatePassword',

  data() {
    return {
      Password2:"",
      PasswordDTO:{
        oldPassword: "",
        newPassword: ""
      }
    }
  },
  methods:{
    save(){},
    pwd2pwd(){
      if (this.Password2 != this.PasswordDTO.newPassword){
        this.$toast("确认密码不一致")
      }
    },


      // 退出登录
  outLogin(){
    var result = UserAPI.OutLogin2()
      // 获取响应结果 
      result.then((val)=>{
        if(val.success){
          // 成功：
          localStorage.removeItem("userDTO")
          localStorage.removeItem("token")
          localStorage.removeItem("userId")
          this.isLogin = false
          this.$router.go(0)
        }else{
          console.log(val.errorMsg)
          // 否则 登录任为真
          this.isLogin = true
        }
            })

  },


    UpdatePwd(){
    var result = UserAPI.UpdatePWS(this.PasswordDTO)
    console.log(this.PasswordDTO)
          // 获取响应结果 
          result.then((val)=>{
            console.log(val)
            if(val.success){
              this.$toast("修改成功！")
              this.outLogin()
              this.$router.push("/userLogin")
            }else{
              console.log(val.errorMsg)
              this.$toast(val.errorMsg)
          
            }
                })
        }
      }
}
</script>

<style>

</style>